﻿using Core;
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Mini/Sys/Conf/Audio")]
public class TblAudio : AbstractSOTable<int, TblAudio.Conf>, IEnumTable
{
    [Serializable]
    public class Conf : ITableConf<int>
    {
        public int Index => Id;

        [HorizontalGroup("row0")]
        public int Id;

        [HorizontalGroup("row0")]
        public string Name;

        public string ResPath;

        public void Awake() { }
    }

    public List<(int, string)> GetEnumList()
    {
        var list = new List<(int, string)>();
        foreach (var conf in Confs)
        {
            list.Add((conf.Id, conf.Name));
        }

        return list;
    }
    
    private readonly Dictionary<string, Conf> dict = new();
    private readonly HashSet<string> missing = new();

    public Conf Find(string key)
    {
        key = key.ToLower();

        if (missing.Contains(key))
        {
            return null;
        }

        if (dict.TryGetValue(key, out var find))
        {
            return find;
        }

        for (var i = 0; i < Confs.Length; i++)
        {
            var conf = Confs[i];
            if (string.Compare(conf.Name.ToLower(), key, StringComparison.Ordinal) == 0)
            {
                dict[key] = conf;
                conf.Awake();
                return conf;
            }
        }

        missing.Add(key);
        return null;
    }
}